package net.openvision.music.ui;

import java.awt.Dimension;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Collection;

import net.openvision.music.model.Bar;
import net.openvision.music.model.DefaultScoreModel;
import net.openvision.music.model.Staff;

/**
 * This class represents a rectangle shaped part of a staff in a musical score
 * that cannot be split up. The ScoreLayoutManager has to ensure that the whole
 * block is laid out in the same line.
 * 
 * @author Hannes Widmoser
 * 
 */
public class StaffBlock {
	protected int staffNr;
	protected Rectangle2D bounds;
	protected BlockType type;
	protected Bar[] content;

	public StaffBlock(Rectangle2D bounds, BlockType type, int staffNr,
			Bar... content) {
		this.bounds = bounds;
		this.type = type;
		this.staffNr = staffNr;
		this.content = content;
	}

	public static Collection<StaffBlock> wrapStaff(BarRenderer renderer,
			DefaultScoreModel model) {
		Collection<StaffBlock> result = new ArrayList<StaffBlock>();
		int i = 0;
		for (Staff s : model.getStaves()) {
			for (Bar b : s.getBars()) {
				Dimension d = renderer.getSize(b);
				result.add(new StaffBlock(new Rectangle2D.Double(0, 0, d
						.getWidth(), d.getHeight()), BlockType.BAR, i, b));
			}
			i++;
		}
		return result;
	}

	/**
	 * Gets the size and position of the block.
	 * 
	 * @return The size and position of the block.
	 */
	public Rectangle2D getBounds() {
		return bounds;
	}

	/**
	 * Sets the size and position of the block.
	 * 
	 * @param bounds
	 *            The new size and position of the block.
	 */
	public void setBounds(Rectangle2D bounds) {
		this.bounds = bounds;
	}

	/**
	 * Every StaffBlock contains a part of a staff. The type of the Block gives
	 * a more close hint what this staff part contains.
	 * 
	 * @return The type of staff part that this block contains.
	 */
	public BlockType getType() {
		return type;
	}

	public int getStaffNr() {
		return staffNr;
	}

	public Bar[] getContent() {
		return content;
	}

}
